Imports Microsoft.Win32

''' <summary>
''' Methods to deal with the system registry.
''' </summary>
Public Class RegistryUtil

#Region "Private constructor"

	Private Sub New()
		' Can't create instance!
	End Sub

#End Region

#Region "Autorun"

	''' <summary>
	''' Gets the full path for the specified AutoRun value.
	''' </summary>
	''' <returns>Full executable path as String, or empty string if not found.</returns>
	Public Shared Function GetAutoRun(ByVal strName As String) As String

		Dim reg As RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)

		If Not reg Is Nothing Then
			Return reg.GetValue(strName, String.Empty).ToString()
		End If

		Return String.Empty

	End Function

	''' <summary>
	''' Inserts a the specified name and path to the AutoRun records.
	''' </summary>
	''' <returns>True if successful, otherwise False.</returns>
	Public Shared Function SetAutoRun(ByVal strName As String, ByVal strPath As String) As Boolean

		Dim reg As RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)

		If Not reg Is Nothing Then

			If String.IsNullOrEmpty(strPath) Then
				reg.DeleteValue(strName, False)
			Else
				reg.SetValue(strName, strPath)
			End If

			Return True

		End If

		Return False

	End Function

#End Region

#Region "General read and write"

	''' <summary>
	''' Reads a specific registry path and key.
	''' </summary>
	''' <returns>Value of registry setting, or emtpy string.</returns>
	Public Shared Function Read(ByVal path As String, ByVal key As String) As String

		Dim reg As RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, False)

		If Not reg Is Nothing Then
			Return reg.GetValue(key, String.Empty).ToString()
		End If

		Return String.Empty

	End Function

	''' <summary>
	''' Writes to the system registry.
	''' </summary>
	''' <returns>True if successful, otherwise False.</returns>
	Public Shared Function Write(ByVal path As String, ByVal key As String, ByVal value As String) As Boolean

		Dim reg As RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, True)

		If Not reg Is Nothing Then

			reg.SetValue(key, value)


			Return True

		End If

		Return False

	End Function

#End Region

End Class